package cn.edu.neusoft.yyd208.foodorder.activity;

import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import cn.edu.neusoft.yyd208.foodorder.R;
import cn.edu.neusoft.yyd208.foodorder.fragment.MeFragment;
import cn.edu.neusoft.yyd208.foodorder.fragment.CollectFragment;
import cn.edu.neusoft.yyd208.foodorder.fragment.ResearchFragment;
import cn.edu.neusoft.yyd208.foodorder.fragment.ShopFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener,ShopFragment.OnFragmentInteractionListener,CollectFragment.OnFragmentInteractionListener,ResearchFragment.OnFragmentInteractionListener,MeFragment.OnFragmentInteractionListener{
    LinearLayout tab_shop;
    LinearLayout tab_collect;
    LinearLayout tab_research;
    LinearLayout tab_me;
    ImageView img_shop;
    ImageView img_collect;
    ImageView img_research;
    ImageView img_me;
    Fragment fragment_shop;
    Fragment fragment_collect;
    Fragment fragment_research;
    Fragment fragment_me;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);
        tab_shop=(LinearLayout)findViewById(R.id.tab_shop);
        tab_collect=(LinearLayout)findViewById(R.id.tab_collect);
        tab_research=(LinearLayout)findViewById(R.id.tab_research);
        tab_me=(LinearLayout)findViewById(R.id.tab_me);
        img_shop=(ImageView)findViewById(R.id.img_shop);
        img_collect=(ImageView)findViewById(R.id.img_collect);
        img_research=(ImageView)findViewById(R.id.img_research);
        img_me=(ImageView)findViewById(R.id.img_me);

    }

    @Override
    void initEvent() {
        tab_shop.setOnClickListener(this);
        tab_collect.setOnClickListener(this);
        tab_research.setOnClickListener(this);
        tab_me.setOnClickListener(this);
        setSelect(0);
    }


    @Override
    void initData() {

    }
    @Override
    public void onClick(View v) {
        //所有图片设成默认的那张
        resetImage();
        switch (v.getId()){
            case R.id.tab_shop:
                setSelect(0);
                break;
            case R.id.tab_collect:
                setSelect(1);
                break;
            case R.id.tab_research:
                setSelect(2);
                break;
            case R.id.tab_me:
                setSelect(3);
                break;
            default:
                break;
        }
        //选择哪组 哪个Fragment显示出来 图片置成press那张

    }
    //所有图片设成默认的那张
    private void resetImage(){
        img_shop.setImageResource(R.drawable.shop_nomal);
        img_collect.setImageResource(R.drawable.collect_nomal);
        img_research.setImageResource(R.drawable.research_nomal);
        img_me.setImageResource(R.drawable.me_nomal);
    }
    private void setSelect(int i){
        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i){
            case 0:
                if (fragment_shop==null){
                    fragment_shop=new ShopFragment();
                    fragmentTransaction.add(R.id.content,fragment_shop);
                }
                else{
                    fragmentTransaction.show(fragment_shop);
                }
                img_shop.setImageResource(R.drawable.shop_press);
                break;
            case 1:
                if (fragment_collect==null){
                    fragment_collect=new CollectFragment();
                    fragmentTransaction.add(R.id.content,fragment_collect);
                }
                else{
                    fragmentTransaction.show(fragment_collect);
                }
                img_collect.setImageResource(R.drawable.collect_press);
                break;
            case 2:
                if (fragment_research==null){
                    fragment_research=new ResearchFragment();
                    fragmentTransaction.add(R.id.content,fragment_research);
                }
                else{
                    fragmentTransaction.show(fragment_research);
                }
                img_research.setImageResource(R.drawable.research_press);
                break;
            case 3:
                if (fragment_me==null){
                    fragment_me=new MeFragment();
                    fragmentTransaction.add(R.id.content,fragment_me);
                }
                else{
                    fragmentTransaction.show(fragment_me);
                }
                img_me.setImageResource(R.drawable.me_press);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }



    private void hideFragment(FragmentTransaction fragmentTransaction){
        if(fragment_shop!=null){
            fragmentTransaction.hide(fragment_shop);
        }
        if(fragment_collect!=null){
            fragmentTransaction.hide(fragment_collect);
        }
        if(fragment_research!=null){
            fragmentTransaction.hide(fragment_research);
        }
        if(fragment_me!=null){
            fragmentTransaction.hide(fragment_me);
        }

    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        String s=uri.toString();
        Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show();
    }
}
